﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace DingChiServiceApp
{
    public class SqlServerSqlBulider : SqlBuilder
    {

        /// <summary>
        ///拼装通用的修改SQL语句
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="obj">类型的对像</param>
        /// <param name="tableName"></param>
        /// <param name="idFieldName">id 字段</param>
        /// <returns>修改SQL语句</returns>
        public static new string GetUpdateSql<T>(T obj, string tableName = null, string idFieldName = "")
        {
            string set = string.Empty;
            string condition = string.Empty;
            Type type = typeof(T);
            FieldInfo[] fieldinfo = type.GetFields();
            object tempObj;
            foreach (FieldInfo p in fieldinfo)
            {
                tempObj = p.GetValue(obj);
                if (tempObj == null)
                {
                    continue;
                }
                if (p.GetType() == typeof(DateTime) && Convert.ToDateTime(tempObj) < Convert.ToDateTime("1753-01-01"))
                {
                    continue;
                }
                if (p.Name.Equals(StringHelper.DbNameToClassName(idFieldName)) || p.Name == "id" || p.Name == "Id")
                {
                    if (tempObj != null && tempObj.ToString().Length > 0)
                    {
                        if (string.IsNullOrEmpty(idFieldName))
                        {

                            condition = "id" + " = " + valueSplitChar + tempObj.ToString() + valueSplitChar;
                        }
                        else
                        {
                            condition = idFieldName + " = " + valueSplitChar + tempObj.ToString() + valueSplitChar;
                        }

                    }
                }
                else
                {
                    String field = StringHelper.LowerCaseFirstLetter(p.Name);
                    String value = String.Empty;
                    if (tempObj is bool || tempObj is Boolean)
                    {
                        value = (bool)tempObj == true ? "1" : "0";
                    }
                    else if (tempObj is DateTime dateTime)
                    {
                        value = dateTime.ToString("yyyy-MM-dd HH:mm:ss");
                    }
                    else
                    {
                        value = tempObj.ToString();
                    }

                    if (set.Length == 0)
                    {
                        set = field + " = " + valueSplitChar + value + valueSplitChar;
                    }
                    else
                    {
                        set += "," + field + " = " + valueSplitChar + value + valueSplitChar;
                    }
                }
            }
            if (condition.Length == 0)
            {
                throw new Exception(buildSqlErrorMessage + " condition = " + condition);
            }
            if (string.IsNullOrEmpty(tableName))
            {
                tableName = GetTableName(obj);
            }
            return string.Format(updateSqlTemplate, tableName, set, condition);
        }


        /// <summary>
        ///拼装通用的插入SQL语句
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="obj">类型的对像</param>
        /// <param name="tableName"></param>
        /// <param name="idFieldName">id 字段</param>
        /// <returns>插入SQL语句</returns>
        public static  string GetInsertSql<T>(T obj, string tableName = "", string idFieldName = "")
        {
            string columNames = string.Empty;
            string values = string.Empty;
            Type type = typeof(T);
            FieldInfo[] fieldInfos = type.GetFields();
            object o;
            foreach (var f in fieldInfos)
            {
                o = f.GetValue(obj);
                if (o == null)
                {
                    continue;
                }
                if (string.IsNullOrEmpty(idFieldName))
                {
                    if (f.Name == "id" || f.Name == "Id")
                    {
                        if (f.FieldType.Equals(typeof(Int64)) || f.FieldType.Equals(typeof(Int32)) || f.FieldType.Equals(typeof(Int16)))
                            if ((Int64)o <= 0) { continue; }
                    }
                }
                else
                {
                    if (f.Name == StringHelper.DbNameToClassName(idFieldName))
                    {
                        if (f.FieldType.Equals(typeof(Int64)) || f.FieldType.Equals(typeof(Int32)) || f.FieldType.Equals(typeof(Int16)))
                            if ((Int64)o <= 0) { continue; }
                    }
                    if (f.Name == "id" || f.Name == "Id")
                    { continue; }
                }

                if (DbHelper.isDebug == true)
                {
                    Console.WriteLine(f.Name + " : " + f.FieldType.Name);
                }
                if (f.FieldType == typeof(DateTime) && Convert.ToDateTime(o) < Convert.ToDateTime("1753-01-01"))
                {
                    continue;
                }
                if (columNames.Length == 0)
                {
                    columNames += StringHelper.CamelCaseToDBnameing(f.Name);
                }
                else
                {
                    columNames += "," + StringHelper.CamelCaseToDBnameing(f.Name);
                }
                var value = String.Empty;
                if (o is bool || o is Boolean)
                {
                    value = (bool)o == true ? "1" : "0";
                }
                else if (o is DateTime dt)
                {
                    value = dt.ToString("yyyy-MM-dd HH:mm:ss");
                }
                else
                {
                    value = o.ToString();
                }

                if (values.Length == 0)
                {
                    values += valueSplitChar + value + valueSplitChar;
                }
                else
                {
                    values += "," + valueSplitChar + value + valueSplitChar;
                }
            }
            if (string.IsNullOrEmpty(tableName))
            {
                tableName = GetTableName(obj);
            }
            return string.Format(insertSqlTemplate, tableName, columNames, values);
        }
    }
}
